<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>async&await</title>
</head>
<body>
<script>
    /*  //语法糖
    * 概念: ES7
    *   async:
    *       返回值是一个Promise,无论函数内是否有await操作
    *       函数内部return返回的值,会成为then回调函数的参数
    *       函数内部如果抛出错误,会被then的第二个参数或catch捕获到
    *
    *   await:
    *       只在async函数出现.普通函数内会报错
    *       正常情况下,await后面是一个promise对象,返回该对象结果.
    *       如果await后面不是Promise,就直接返回对应的值.
    *
    * */

    {
        let p1 = ()=> new Promise((resolve, reject) => resolve("hello"));
        function fn() {
            p1().then(
                res => {
                    console.log("demo1-1",res)
                }
            );
            console.log("demo1-2")
        }
        fn();
    }

    {
        let p1= ()=>Promise.resolve("hello");
        async function fn() {
            let res = await p1();
            //感觉像是同步代码
            //await获取的是resolve()的结果;
            console.log("demo2-1",res);
            console.log("demo2-2")
        }
        fn();
    }
    {
        let p1= ()=>Promise.resolve("hello");
        async function fn() {
            return await p1();
        }
        //async函数返回的是一个promise
        fn().then(
            res=>{
                console.log("demo3",res)
            }
        );
    }
    {
        let p1= ()=>Promise.resolve("hello");
        async function fn() {
            return await p1();
        }
        //async函数返回的是一个promise
        fn().then(
            res=>{
                console.log("demo3",res)
            }
        );
    }

    {
        /*getFaceResult (a) {
            let b =0;
                try {
                    ...
                } catch(err) {
                    ...
                }
            return a + b
        }*/
    }
    {
        //串行
        let getP1 = ()=>{ //10s
//            return Promise.resolve("p1")
            return new Promise((resolve)=>{
                setTimeout(resolve, 3000 , "p1")
            })
        };
        let getP2 = ()=>{ //5s
//            return Promise.resolve("p2")
            return new Promise((resolve)=>{
                setTimeout(resolve, 5000 , "p2")
            })
        };

        let fn = async ()=>{
            let p1Value = await getP1();
            let p2Value = await getP2();
            console.log(p1Value, p2Value)  //10+5
        };
        fn();

    }
    {
        //并行
        let getP1 = ()=>{ //10s
            return Promise.resolve("p1")
        };
        let getP2 = ()=>{ //5s
            return Promise.resolve("p2")
        };

        let fn = async ()=>{
            let doP1 = getP1();
            let doP2 = getP2();
            let p1Value = await doP1;
            let p2Value = await doP2;
            console.log(p1Value, p2Value)  //10+5
        };
        fn();

    }

    {
        //并行写法
        let getP1 = ()=>{
            return Promise.resolve("p1")
        };
        let getP2 = ()=>{
            return Promise.resolve("p2")
        };
        let fn = async ()=>{
            let values = await Promise.all([getP1(),getP2()]);  //10s
            console.log(values) //10s
        };
        fn()
    }












</script>
</body>
</html>